﻿using KinonekoSoftware.CodariaDB.Documents.DataParts.Modules;


namespace KinonekoSoftware.Studio.Models.Modules
{

    public sealed class TextGroupPresenter : GroupPresenter<Block_Text>, IGroupedPresenter
    {
        private readonly Dictionary<string, TextElementPresenter> _dictionary;
        
        
        public TextGroupPresenter(Block_Text target, IModuleMessageBus queue)
        {
            _dictionary = new Dictionary<string, TextElementPresenter>();
            Queue       = queue ?? throw new ArgumentNullException(nameof(queue));
            Items       = new ObservableCollection<TextElementPresenter>();
            Initialize(target);
        }

        protected override void OnInitialize(Block_Text target)
        {
            //
            // 清除
            Items.Clear();

            //
            // 添加
            foreach (var item in target.Items.Select(Select))
            {
                if (!string.IsNullOrEmpty(item.Metadata))
                {
                    _dictionary.TryAdd(item.Metadata, item);
                }

                Items.Add(item);
            }
        }
        
        public override string GetTitle() => string.IsNullOrEmpty(Title) ? CodariaSR.Module_Block_Text : Title;
        public override bool IsMultipleVariants() => true;
        public override IEnumerable<Variant> GetVariants(string documentID) => Items.Select(x => x.GetVariant(documentID));
        public override Variant GetVariant(string documentID) => null;
        
        public override void DataSync(Variant variant)
        {
            if (!string.IsNullOrEmpty(variant.Metadata) &&
                variant is StringVariant sv             &&
                sv.Type == VariantType.Color            &&
                _dictionary.TryGetValue(sv.Metadata, out var color))
            {
                color.Value = sv.Value;
            }
        }

        
        #region IGroupedPresenter

        

        public IEnumerable<ModulePresenter> GetElements() => Items;

        public ModuleBlock GetBlock() => AssociatedObject;


        public ModulePresenter NewItem()
        {
            var item = new Element_Text
            {
                Id          = ID.Get(),
                Fallback    = "Untitled",
            };
            var p = Select(item);

            AssociatedObject.Items.Add(item);
            Items.Add(p);
            return p;
        }

        public void ShiftUpItem(ModulePresenter target)
        {
            var target2 = (TextElementPresenter)target;
            var element = target2.AssociatedObject;

            //
            //
            AssociatedObject.Items.ShiftUp(element);
            Items.ShiftUp(target2);

        }

        public void ShiftDownItem(ModulePresenter target)
        {
            var target2 = (TextElementPresenter)target;
            var element = target2.AssociatedObject;

            //
            //
            AssociatedObject.Items.ShiftDown(element);
            Items.ShiftDown(target2);
        }

        public void RemoveItem(ModulePresenter target)
        {
            var target2 = (TextElementPresenter)target;
            var element = target2.AssociatedObject;

            //
            //
            AssociatedObject.Items.Remove(element);
            Items.Remove(target2);
        }
        
        #endregion

        protected override Task<bool> OnEditItem() => null;
        protected override void OnIndexChanged(int oldIndex, int newIndex)
        {
            AssociatedObject.Index = newIndex;
        }


        public TextElementPresenter Select(Element_Text text) => new TextElementPresenter
        {
            AssociatedObject = text,
            Queue            = Queue,
            TopLevel         = this,
        };

        protected override Block_Text CreateDummyInstance() => Block_Text.CreateInstance();

        public ObservableCollection<TextElementPresenter> Items { get; }
    }
}